<template>
  <div class="register-container">
    <!-- 顶部导航 -->
    <div class="top_nav">
      <div class="nav-icon" @click="toggleLanguage">
        <van-icon name="globe-o" class="globe-icon" />
      </div>
      <div class="nav-icon" @click="toLogin">
        <img src="../assets/my_right.png" alt="" />
      </div>
    </div>

    <!-- 注册框 -->
    <div class="register-box">
      <div class="logo">{{ $t("register.title") }}</div>

      <van-form @submit="onSubmit">
        <!-- 邮箱输入框 -->
        <van-field
          v-model="email"
          name="email"
          type="email"
          :placeholder="$t('register.email')"
          :rules="[{ required: true, message: $t('register.emailRequired') }]"
        >
          <template #left-icon>
            <van-icon name="envelop-o" class="email-icon" />
          </template>
        </van-field>

        <!-- 密码输入框 -->
        <van-field
          v-model="password"
          name="password"
          :type="showPassword ? 'text' : 'password'"
          :placeholder="$t('register.password')"
          :rules="[
            { required: true, message: $t('register.passwordRequired') },
          ]"
        >
          <template #left-icon>
            <van-icon name="lock" />
          </template>
          <template #right-icon>
            <van-icon
              :name="showPassword ? 'eye-o' : 'closed-eye'"
              class="eye-icon"
              @click="togglePassword"
            />
          </template>
        </van-field>

        <!-- 确认密码输入框 -->
        <van-field
          v-model="confirmPassword"
          name="confirmPassword"
          :type="showPassword ? 'text' : 'password'"
          :placeholder="$t('register.confirmPassword')"
          :rules="[
            { required: true, message: $t('register.confirmPasswordRequired') },
            {
              validator: validatePasswords,
              message: $t('register.passwordsNotMatch'),
            },
          ]"
        >
          <template #left-icon>
            <van-icon name="lock" />
          </template>
          <template #right-icon>
            <van-icon
              :name="showPassword ? 'eye-o' : 'closed-eye'"
              class="eye-icon"
              @click="togglePassword"
            />
          </template>
        </van-field>

        <!-- 验证码 -->
        <van-field
          v-model="inviteCode"
          name="inviteCode"
          type="text"
          :placeholder="$t('register.inviteCode')"
          :rules="[
            { required: true, message: $t('register.inviteCodeRequired') },
          ]"
        >
          <template #left-icon>
            <van-icon name="lock" />
          </template>
          <template #right-icon>
            <van-icon
              :name="showPassword ? 'eye-o' : 'closed-eye'"
              class="eye-icon"
              @click="togglePassword"
            />
          </template>
        </van-field>

        <!-- 注册按钮 -->
        <div style="margin: 16px">
          <van-button
            round
            block
            type="primary"
            native-type="submit"
            class="register-btn"
          >
            {{ $t("register.register") }}
          </van-button>
        </div>
      </van-form>

      <!-- 登录链接 -->
      <div class="login-link">
        {{ $t("register.login") }}
        <a href="/login">{{ $t("register.toLogin") }}</a>
      </div>
    </div>
  </div>
</template>

<script setup name="Register">
import { ref } from "vue";
import { useI18n } from "vue-i18n";
import { Icon, Form, Field, Button, Toast } from "vant";
import { useRouter } from "vue-router";
import { UserApi } from "@/api/user.api";
const { t, locale } = useI18n();
const router = useRouter();
const email = ref("");
const password = ref("");
const confirmPassword = ref("");
const inviteCode = ref("");
const showPassword = ref(false);

const togglePassword = () => {
  showPassword.value = !showPassword.value;
};
const toLogin = () => {
  router.push("/login");
};
const toggleLanguage = () => {
  const newLocale = locale.value === "zh" ? "en" : "zh";
  locale.value = newLocale;
  localStorage.setItem("locale", newLocale);
};

const validatePasswords = (value) => {
  return value === password.value;
};

const onSubmit = (values) => {
  Toast(t("register.registering"));
  // 这里添加注册逻辑
  const registerData = {
    email: email.value,
    password: password.value,
    invitationCode: inviteCode.value,
  };
  UserApi.register(registerData).then((res) => {
    if (res.code == 200) {
      Toast(t("register.registerSuccess"));
      router.push("/login");
    } else {
      Toast(t("register.registerFailed"));
    }
  });
};
</script>

<style scoped>
.register-container {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f5f5f5;
  padding: 20px;
  box-sizing: border-box;
}
.top_nav {
  position: absolute;
  top: 20px;
  left: 0;
  right: 0;
  display: flex;
  justify-content: space-between;
  padding: 0 20px;
}
.nav-icon {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.nav-icon img {
  width: 24px;
  height: 24px;
  object-fit: contain;
}
.globe-icon {
  width: 24px;
  height: 24px;
  background: url(../assets/my_left.svg) center / contain no-repeat;
}
.service-icon {
  width: 24px;
  height: 24px;
  object-fit: contain;
}
.login-link {
  text-align: center;
  font-size: 14px;
  color: #666;
}
.login-link a {
  color: #4460f6;
  text-decoration: none;
}
.register-box {
  width: 100%;
  max-width: 400px;
  background: white;
  border-radius: 16px;
  padding: 30px 20px;
  box-shadow: 0 2px 12px #0000000a;
}
.logo {
  text-align: center;
  font-size: 24px;
  font-weight: 700;
  margin-bottom: 30px;
}
.form-item {
  margin-bottom: 20px;
}
.input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
  background: #f8f9fa;
  border-radius: 25px;
  padding: 12px 15px;
}
</style>
